﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

namespace VoteList.Layouts.VoteList
{
    public partial class comment : LayoutsPageBase
    {
        public static Guid mSiteId = Guid.NewGuid();
        public static Guid mWebId = Guid.NewGuid();
        public static string mCurrentUser = string.Empty;
        public static string mToUser = string.Empty;
        public const string mlistName = "commentmessage";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mSiteId = SPContext.Current.Site.ID;
                mWebId = SPContext.Current.Web.ID;
                mCurrentUser = SPContext.Current.Web.CurrentUser.LoginName;
                if (Request.QueryString["user"] != null)
                {
                    mToUser = Request.QueryString["user"];
                }


                BindData();
            }
            this.btnAddComment.Click += new EventHandler(btnAddComment_Click);

        }


        public void BindData()
        {
            try
            {
                DataTable dt = GetMessage();
                this.repeaterItems.DataSource = dt;
                repeaterItems.DataBind();
            }
            catch { }
        }

        /// <summary>
        /// Get all Candidates
        /// </summary>
        /// <returns>DataTable</returns>
        public DataTable GetMessage()
        {

            DataTable dt = new DataTable();

            DataColumn column = new DataColumn("Name");
            DataColumn column2 = new DataColumn("Comment");
            DataColumn column3 = new DataColumn("LoginName");
            DataColumn column4 = new DataColumn("Time");


            dt.Columns.Add(column);
            dt.Columns.Add(column2);
            dt.Columns.Add(column3);
            dt.Columns.Add(column4);

            if (string.IsNullOrEmpty(mToUser))
            {
                return dt;
            }
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(mSiteId))
                {
                    using (SPWeb web = site.OpenWeb(mWebId))
                    {
                        SPList list = web.Lists[mlistName];
                        SPQuery query = new SPQuery();
                        query.Query = @"<Where><Eq><FieldRef Name='To' /><Value Type='Text'>" + mToUser + "</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='TRUE' /></OrderBy>";

                        SPListItemCollection itemCollection = list.GetItems(query);
                        foreach (SPListItem item in itemCollection)
                        {
                            try
                            {
                                DataRow row = dt.NewRow();
                                string loginName = item["From"].ToString();
                                string name = EnsureName(loginName);
                                if (string.IsNullOrEmpty(name))
                                {
                                    row[0] = loginName;
                                }
                                else
                                {
                                    row[0] = name;
                                }

                                row[1] = item["Title"].ToString();
                                row[2] = loginName;
                                dt.Rows.Add(row);
                            }
                            catch { }
                        }
                    }
                }
            });

            return dt;
        }

        private string EnsureName(string loginName)
        {
            string name = string.Empty;
            SPUser user = SPContext.Current.Web.EnsureUser(loginName);
            if (user != null)
            {
                name = user.Name;
            }
            return name;
        }

        void btnAddComment_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbxComment.Text))
            {
                return;
            }
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(mSiteId))
                {
                    using (SPWeb web = site.OpenWeb(mWebId))
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList list = web.Lists[mlistName];
                        SPListItem item = list.Items.Add();
                        item["From"] = mCurrentUser;
                        item["Title"] = this.tbxComment.Text;
                        item["To"] = mToUser;
                        item.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });

            this.tbxComment.Text = string.Empty;
            BindData();
        }
    }
}
